﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using FinalCountdownServices.Extensions;
using FinalCountdownServices.Models;

namespace FinalCountdownServices
{

    public class StorageMessage
    {
        public StorageMessage(bool success)
        {
            Success = success;
        }

        public StorageMessage(string errorMessage)
        {
            ErrorMessage = errorMessage;
            Success = false;
        }

        public bool Success { get; set; }
        public string ErrorMessage { get; set; }
    }

    public interface IEventService
    {
        List<EventModel> MyEvents { get; set; }
        void AddEvent(EventModel e);
        EventModel GetEvent(int id);
        void ModifyEvent(EventModel e);
        void RemoveEvent(int id);
        int GetNextId();
    }

    public class EventService : IEventService
    {
        private IsolatedStorageSettings _storage;

        private IReminderService _reminderService;

        public EventService(IReminderService reminderService)
        {
            Init();
            _reminderService = reminderService;

        }

        private void Init()
        {
            MyEvents = new List<EventModel>();
            _storage = IsolatedStorageSettings.ApplicationSettings;
            Load();
        }

        public List<EventModel> MyEvents { get; set; }

        public void AddEvent(EventModel e)
        {
            var nextId = GetNextId();
            e.Id = nextId;
            MyEvents.Add(e);
            Save();
        }

        public EventModel GetEvent(int id)
        {
            var myEvent = MyEvents.FirstOrDefault(x => x.Id == id);
            if (myEvent == null)
                throw new Exception("Event not found");
            return myEvent;
        }


        public void ModifyEvent(EventModel e)
        {
            var eventToModify = MyEvents.FirstOrDefault(x => x.Id == e.Id);
            MyEvents.Remove(eventToModify);
            MyEvents.Add(e);
            Save();
        }

        public void RemoveEvent(int id)
        {
            var eventToRemove = MyEvents.FirstOrDefault(x => x.Id == id);
            MyEvents.Remove(eventToRemove);
            Save();
        }

        public int GetNextId()
        {
            var maxId = MyEvents.Select(x => x.Id).Max();
            var nextId = (maxId ?? 0) + 1;
            return nextId;
        }

        private void Load()
        {
            var events = _storage.Get(MyEvents);
            MyEvents = events;
        }

        private void Save()
        {
            foreach (var e in MyEvents)
            {
                if (e.NotificationEnabled)
                    _reminderService.AddOrModifyReminder(e);
                else
                    _reminderService.RemoveReminder(e);

            }

            _storage.Set(MyEvents);
        }
    }
}
